//
//  DetailViewController.m
//  iNews
//
//  Created by lanou3g on 14-12-10.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "DetailViewController.h"
#import "DetailWebView.h"
#import "DetailModel.h"
#import "ShareDataHandle.h"

#import "ArchiveHandleSingle.h"

//static NSString *kButtonTitle = @"收藏";

//分别为空的,实的,没圆圈的
static NSString *imag1 = @"icon_littlesStar@2x.png";
static NSString *imag2 = @"icon_star_full@2x.png";
//static NSString *imag3 = @"icon_star123_yellow@2x.png";


@interface DetailViewController ()

@property(nonatomic, retain)UIActivityIndicatorView *myAIV;

@end

@implementation DetailViewController

- (void)dealloc
{
    [_webV release];
   
    [_docidStr release];
    [_myAIV release];
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        
        self.webV = [[[DetailWebView alloc] initWithFrame:self.view.bounds] autorelease];
        
        //添加动画
        self.myAIV = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        self.myAIV.center = CGPointMake([UIScreen mainScreen].bounds.size.width /2, 100);
        [self.webV addSubview:self.myAIV];
        [self.myAIV startAnimating];
        
        [self addObserver:self forKeyPath:@"docidStr" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
       
    }
    return self;
}

- (void)loadView
{
    self.view = self.webV;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   
  //  self.ima = [UIImage imageNamed:imag1];

  //  self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:self.ima style:UIBarButtonItemStyleDone target:self action:@selector(barButtonAction:)];
   
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    
    if ([keyPath isEqualToString:@"docidStr"]) {
        
        //先看沙盒里面存不存在,存在,则buttonTitle设为取消收藏
        [[ArchiveHandleSingle archiveSingle] unachiveArray];
        int t = 0;
        //收藏了,下次进入,还是显示收藏
        for (DetailModel *m in [ArchiveHandleSingle archiveSingle].articleArray) {
            if ([m.docid isEqualToString:self.docidStr]) {
                
                //如果是从收藏进来的
                self.dM = m;
             //   kButtonTitle = @"取消收藏";
                
                self.ima = [UIImage imageNamed:imag2];
                
                self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:self.ima style:UIBarButtonItemStyleDone target:self action:@selector(barButtonAction:)] autorelease];
                
                t = 1;
                [self.webV getThml:m];
                
                [self.myAIV stopAnimating];
                [self.myAIV release];
                
                break;
            }
        }
        
        if (!t) {
            
          //  kButtonTitle = @"收藏";
            self.ima = [UIImage imageNamed:imag1];
            
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:self.ima style:UIBarButtonItemStyleDone target:self action:@selector(barButtonAction:)];
        
        
        [[ShareDataHandle shareDataHandle] parseDetailDataWithParam:self.docidStr];
        
        [[ShareDataHandle shareDataHandle] addObserver:self forKeyPath:@"detailArray" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
         
            
        }
    }
    
    if ([keyPath isEqualToString:@"detailArray"]) {
        
        DetailModel *dm = [ShareDataHandle shareDataHandle].detailArray.lastObject;
        
        [self.webV getThml:dm];
        
        //关闭动画
        [self.myAIV stopAnimating];
        [self.myAIV release];
        
        [[ShareDataHandle shareDataHandle] removeObserver:self forKeyPath:@"detailArray"];
        
        //不能移除
      //  [[ShareDataHandle shareDataHandle] removeObserver:self forKeyPath:@"docidStr"];
    }
}

#pragma mark 点击收藏
- (void)barButtonAction:(UIBarButtonItem *)sender
{
  //  NSLog(@"%@",self.ima);
    if ([self.ima isEqual:[UIImage imageNamed:imag1]]) {
        
        DetailModel *dm = [[ShareDataHandle shareDataHandle] detailArray].lastObject;
        
        if (dm != nil) {
            
            [[ArchiveHandleSingle archiveSingle] archiveModel:dm];
            [self addAlterView];
            
            //  kButtonTitle = @"取消收藏";
            self.ima = [UIImage imageNamed:imag2];
            [sender setImage:self.ima];
            
        }else{
            
            UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"暂无网络连接" message:@"无法收藏" delegate:self cancelButtonTitle:nil
                                               otherButtonTitles:nil, nil];
            [av show];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [av dismissWithClickedButtonIndex:0 animated:YES];
                [av release];
            });
        }
        
    }else{
        //取消收藏是删除沙盒里面的
        [[ArchiveHandleSingle archiveSingle] deleteDMValue:self.dM];
        [self addAlterView];
       // kButtonTitle = @"收藏";
        self.ima = [UIImage imageNamed:imag1];
        [sender setImage:self.ima];
    }
}

#pragma mark 添加弹窗
- (void)addAlterView
{
    if ([self.ima isEqual:[UIImage imageNamed:imag1]]) {
        
        UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"收藏成功!" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        
        [alertV show];

    }else{
        
        UIAlertView *aV = [[UIAlertView alloc] initWithTitle:@"取消成功" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        
        [aV show];
    }
    
}





- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
